package com.example.environment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.environment.pojo.*;
import com.example.environment.pojo.mapper.*;
import com.example.environment.service.FeedbackService;
import com.example.environment.service.SupervisorService;
import com.example.environment.tools.common.ResultMsg;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements FeedbackService {
    @Autowired
    private FeedbackMapper feedbackMapper;
    @Autowired
    private SupervisorMapper supervisorMapper;
    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private ProvinceMapper provinceMapper;



    @Autowired
    private SupervisorService supervisorService;
    public ResultMsg saveById(Feedback feedback,String id){
        feedback.setId(UUID.randomUUID().toString());
        feedback.setSvId(id);
        feedback.setState(0);
        if(save(feedback)){
            return ResultMsg.successOp("新建反馈成功");
        }
        return ResultMsg.failOp("新建反馈失败");
    }

    public ResultMsg  getAllFeedbackBySupervisor(String id){
        LambdaQueryWrapper<Feedback> queryWrapper = Wrappers.lambdaQuery(Feedback.class)
                .eq(Feedback::getSvId, id);
        List<Feedback> FeedbackList = feedbackMapper.selectList(queryWrapper);
        System.out.println(FeedbackList);
        if(FeedbackList==null){
            return ResultMsg.failOp("该公众监督员反馈列表为空");
        }

        // 查询 City 信息
        Set<Integer> cityIds = FeedbackList.stream()
                .map(Feedback::getCityId)
                .collect(Collectors.toSet());
        Map<Integer, City> cityMap = cityMapper.selectBatchIds(cityIds).stream()
                .collect(Collectors.toMap(City::getId, Function.identity()));

        // 查询 Province 信息
        Set<Integer> provinceIds = FeedbackList.stream()
                .map(Feedback::getProvinceId)
                .collect(Collectors.toSet());
        Map<Integer, Province> provinceMap = provinceMapper.selectBatchIds(provinceIds).stream()
                .collect(Collectors.toMap(Province::getId, Function.identity()));

        // 整合数据到 List<Map<String, Object>>
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (Feedback feedback : FeedbackList) {
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("id", feedback.getId());
            dataMap.put("date", feedback.getDate());
            dataMap.put("time", feedback.getTime());
            dataMap.put("feedbackAqi", feedback.getFeedbackAqi());
            dataMap.put("address", feedback.getAddress());
            dataMap.put("content", feedback.getContent());

            City city = cityMap.get(feedback.getCityId());
            if (city != null) {
                dataMap.put("city", city.getCity());
            }

            Province province = provinceMap.get(feedback.getProvinceId());
            if (province != null) {
                dataMap.put("province", province.getProvince());
            }

            resultList.add(dataMap);
        }

        return ResultMsg.successOp(resultList);
    }


    public ResultMsg updateFeedbackById(Feedback feedback){
        boolean result = updateById(feedback); // 调用 updateById 方法
        if (result) {
                return ResultMsg.successOp("更新反馈状态成功");
        }
        return ResultMsg.failOp("更新反馈状态失败");

    }

    public  List<Map<String, Object>> getAllFeedback(){
        // 查询符合 gsId 的所有 Feedback 记录
        List<Feedback> feedbackList = feedbackMapper.selectList(null);

        Set<Integer> cityIds = feedbackList.stream().map(Feedback::getCityId).collect(Collectors.toSet());
        Set<Integer> provinceIds = feedbackList.stream().map(Feedback::getProvinceId).collect(Collectors.toSet());

        // 根据 feedbackIds 查询 Feedback 信息
        Set<String> svIds = feedbackList.stream()
                .map(Feedback::getSvId)
                .collect(Collectors.toSet());

        // 根据 svIds 查询 Supervisor 信息
        Map<String, Supervisor> supervisorMap = supervisorMapper.selectBatchIds(svIds).stream()
                .collect(Collectors.toMap(Supervisor::getId, Function.identity()));

        // 根据 cityIds 查询 City 信息
        Map<Integer, City> cityMap = cityMapper.selectBatchIds(cityIds).stream()
                .collect(Collectors.toMap(City::getId, Function.identity()));

        // 根据 provinceIds 查询 Province 信息
        Map<Integer, Province> provinceMap = provinceMapper.selectBatchIds(provinceIds).stream()
                .collect(Collectors.toMap(Province::getId, Function.identity()));

        // 整合数据到 FeedbackVo
        List<Map<String, Object>> FeedbackMap = new ArrayList<>();
        int i=0;
        for (Feedback feedback : feedbackList) {
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("id", feedback.getId());
            dataMap.put("date", feedback.getDate());
            dataMap.put("aqi", feedback.getFeedbackAqi());
            dataMap.put("time", feedback.getTime());
            dataMap.put("address", feedback.getAddress());
            dataMap.put("content", feedback.getContent());
            Supervisor supervisor = supervisorMap.get(feedback.getSvId());
            if (supervisor != null) {
                    dataMap.put("svPhone", supervisor.getPhone());
                    dataMap.put("svName", supervisor.getName());
                    dataMap.put("svBirthday", supervisor.getBirthday());
                    dataMap.put("gender", supervisor.getGender());
            }
            City city = cityMap.get(feedback.getCityId());
            if (city != null) {
                dataMap.put("city", city.getCity());
            }

            Province province = provinceMap.get(feedback.getProvinceId());
            if (province != null) {
                dataMap.put("province", province.getProvince());
            }
            FeedbackMap.add(dataMap);
            i++;
        }

        return FeedbackMap;

    }

    @Override
    public List<Map<String, Object>> getAllFeedbackByState(int state) {
        LambdaQueryWrapper<Feedback> queryWrapper = Wrappers.lambdaQuery(Feedback.class)
                .eq(Feedback::getState, state);
        List<Feedback> FeedbackList = feedbackMapper.selectList(queryWrapper);
        System.out.println(FeedbackList);
        if(FeedbackList==null){
            return null;
        }
        // 根据 feedbackIds 查询 Feedback 信息
        Set<String> svIds = FeedbackList.stream()
                .map(Feedback::getSvId)
                .collect(Collectors.toSet());

        // 根据 svIds 查询 Supervisor 信息
        Map<String, Supervisor> supervisorMap = supervisorMapper.selectBatchIds(svIds).stream()
                .collect(Collectors.toMap(Supervisor::getId, Function.identity()));

        // 查询 City 信息
        Set<Integer> cityIds = FeedbackList.stream()
                .map(Feedback::getCityId)
                .collect(Collectors.toSet());
        Map<Integer, City> cityMap = cityMapper.selectBatchIds(cityIds).stream()
                .collect(Collectors.toMap(City::getId, Function.identity()));

        // 查询 Province 信息
        Set<Integer> provinceIds = FeedbackList.stream()
                .map(Feedback::getProvinceId)
                .collect(Collectors.toSet());
        Map<Integer, Province> provinceMap = provinceMapper.selectBatchIds(provinceIds).stream()
                .collect(Collectors.toMap(Province::getId, Function.identity()));

        // 整合数据到 List<Map<String, Object>>
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (Feedback feedback : FeedbackList) {
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("id", feedback.getId());
            dataMap.put("date", feedback.getDate());
            dataMap.put("time", feedback.getTime());
            dataMap.put("feedbackAqi", feedback.getFeedbackAqi());
            dataMap.put("address", feedback.getAddress());
            dataMap.put("content", feedback.getContent());
            Supervisor supervisor = supervisorMap.get(feedback.getSvId());
            if (supervisor != null) {
                dataMap.put("svPhone", supervisor.getPhone());
                dataMap.put("svName", supervisor.getName());
                dataMap.put("svBirthday", supervisor.getBirthday());
                dataMap.put("gender", supervisor.getGender());
            }
            City city = cityMap.get(feedback.getCityId());
            if (city != null) {
                dataMap.put("city", city.getCity());
            }
            Province province = provinceMap.get(feedback.getProvinceId());
            if (province != null) {
                dataMap.put("province", province.getProvince());
            }
            resultList.add(dataMap);
        }

        return resultList;
    }
}
